import os

import pyecharts.options as opts
from pyecharts.charts import Grid
from pyecharts.charts import Line

from StockPro.ClassStock import Stock


def analyzedata(tdata: Stock, tmacd, tk, td, tj, tcci, code):
    macd = tmacd[::-1]
    k = tk[::-1]
    d = td[::-1]
    j = tj[::-1]
    cci = tcci[::-1]
    data = Stock()
    data.name = tdata.name
    data.date = tdata.date[::-1]
    data.days = tdata.days
    data.highprice = tdata.highprice[::-1]
    data.lowprice = tdata.lowprice[::-1]
    data.closeprice = tdata.closeprice[::-1]
    try:
        for i in range(data.days):
            macd[i] = round(macd[i], 2)
            k[i] = round(k[i], 2)
            d[i] = round(d[i], 2)
            j[i] = round(j[i], 2)
            data.closeprice[i] = round(data.closeprice[i], 2)
        for i in range(len(cci)):
            cci[i] = round(cci[i], 2)
        linecloseprice = (Line(
            init_opts=opts.InitOpts(
                width='1200px', height='800px')

        )
            .add_xaxis(data.date)
            .add_yaxis('股价', data.closeprice, is_smooth=True, xaxis_index=0, yaxis_index=0)
            .set_global_opts(
            title_opts=opts.TitleOpts(title=f'{data.name}的股票行情',
                                      title_textstyle_opts=opts.TextStyleOpts(color='red')),
            xaxis_opts=opts.AxisOpts(name='时间', type_='category', min_=data.date[0]),
            yaxis_opts=opts.AxisOpts(name='收盘价', split_number=10),
            datazoom_opts=opts.DataZoomOpts(range_start=90, range_end=100, xaxis_index=[0, 1, 2, 3], pos_right='center',
                                            pos_top='95%'),
            legend_opts=opts.LegendOpts(pos_bottom='95%', pos_right='70%')
        )
        )
        linekdj = (Line(
            init_opts=opts.InitOpts(
                width='1200px', height='800px')

        )
            .add_xaxis(data.date)
            .add_yaxis('K', k, is_smooth=True, color='green', xaxis_index=1, yaxis_index=1)
            .add_yaxis('D', d, is_smooth=True, color='blue', xaxis_index=1, yaxis_index=1)
            .add_yaxis('J', j, is_smooth=True, color='red', xaxis_index=1, yaxis_index=1)
            .set_global_opts(
            xaxis_opts=opts.AxisOpts(name='时间', type_='category'),
            yaxis_opts=opts.AxisOpts(name='KDJ值', split_number=10),
            legend_opts=opts.LegendOpts(pos_top='50%', pos_right='65%')
        )

        )
        linemacd = (Line(
            init_opts=opts.InitOpts(
                width='1200px', height='800px')

        )
            .add_xaxis(data.date)
            .add_yaxis('MACD', macd, is_smooth=True, color='red', xaxis_index=2, yaxis_index=2)
            .set_global_opts(
            xaxis_opts=opts.AxisOpts(name='时间', type_='category'),
            yaxis_opts=opts.AxisOpts(name='MACD值', split_number=10),
            legend_opts=opts.LegendOpts(pos_left="70%", pos_bottom='95%')
        )

        )
        linecci = (Line(
            init_opts=opts.InitOpts(
                width='1200px', height='800px')

        )
            .add_xaxis(data.date)
            .add_yaxis('CCI', cci, is_smooth=True, color='red', xaxis_index=3, yaxis_index=3)
            .set_global_opts(
            xaxis_opts=opts.AxisOpts(name='时间', type_='category'),
            yaxis_opts=opts.AxisOpts(name='CCI值', split_number=10),
            legend_opts=opts.LegendOpts(pos_left="70%", pos_top='50%')

        )

        )
        grid = Grid(init_opts=opts.InitOpts(width='1200px', height='800px'))
        grid.add(linecloseprice, grid_opts=opts.GridOpts(pos_bottom='55%', pos_right='55%', is_contain_label=True,
                                                         tooltip_opts=opts.TooltipOpts(trigger="axis",
                                                                                       axis_pointer_type="cross"), ),
                 grid_index=0,
                 is_control_axis_index=True)
        grid.add(linekdj, grid_opts=opts.GridOpts(pos_top='55%', pos_right='55%', is_contain_label=True,
                                                  tooltip_opts=opts.TooltipOpts(trigger="axis",
                                                                                axis_pointer_type="cross"), ),
                 grid_index=1,
                 is_control_axis_index=True)
        grid.add(linemacd, grid_opts=opts.GridOpts(pos_left="55%", pos_bottom='55%', is_contain_label=True,
                                                   tooltip_opts=opts.TooltipOpts(trigger="axis",
                                                                                 axis_pointer_type="cross"), ),
                 grid_index=2,
                 is_control_axis_index=True)
        grid.add(linecci, grid_opts=opts.GridOpts(pos_left="55%", pos_top='55%', is_contain_label=True,
                                                  tooltip_opts=opts.TooltipOpts(trigger="axis",
                                                                                axis_pointer_type="cross"), ),
                 grid_index=3,
                 is_control_axis_index=True)
        if not os.path.exists('./charts'):
            os.makedirs('./charts')
        grid.render('./charts/' + code + '.html')
    except:
        pass
